<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use fast\Random;
use phpCAS;
use think\Session;
use think\helper\Time;
use app\admin\model\Outsidestudent as outside;
use think\Config;
use think\Db;
use app\admin\model\ReceptionUnit;
use think\Validate;

class Outsidestudent extends Frontend
{
    protected $noNeedLogin = ['index','view','search'];
    protected $noNeedRight = '*';
    protected $layout = 'weiui';


    public function _initialize(){

        parent::_initialize();
        $this->model = new outside();
        $action = strtolower(request()->action());

    }

    public function index()
    {
        if ($this->request->isPost()){
            $params = $this->request->post("row/a");

            // 判断是否已经预约
            // echo "<pre>";
            // echo $params['admission_time']."###########".$params['tel'];
            // exit();
            if(strtotime(date('Y-m-d'))> strtotime($params['admission_time']) )
            {
                $this->error('请选择合适的入校时间');
            }

            $params['status'] = 0;
            // 根据手机号判断
            $info = $this->model->where(['tel'=>$params['tel']])->find();
            if(isset($info)){
                $row= $this->model->get($info['id']);
                $ret = $row->allowField(true)->save($params);
            }else
                $ret = $this->model->allowField(true)->save($params);
            
            $this->success('预约成功','', ['nickname' => $params['nickname']]);
            
        }

        $title = '入校预约';
        $this->assign('pageTitle', $title);
        return $this->fetch();
    }


    public function view()
    {
    
        $title = '入校预约';
        $this->assign('pageTitle', $title);
        return $this->fetch();
    }


    public function search()
    {
        if ($this->request->isPost()){
            $nickname = $this->request->post("nickname");
            $tel = $this->request->post("tel");

            $info = $this->model->where(['tel'=>$tel,'nickname'=>$nickname])->find();
            if(isset($info['status']))
            {
                switch ($info['status']) {
                    case '1':
                        $info['statustext'] = '您好，你的预约已通过，请扫码进入！';
                        break;    
                    case '2':
                        $info['statustext'] = '您好，你的预约已驳回，请重新预约！';
                        break; 
                    default:
                        $info['statustext'] = '您好，你的预约正在审核中，请等待审核！';
                        break;
                }
            }   
            $this->success('预约成功','', ['nickname' => $info]);
        }
        $title = '入校预约';
        $this->assign('pageTitle', $title);
        return $this->fetch();
    }

}